package yuan.zk.watcher;

import org.apache.zookeeper.KeeperException;
import org.apache.zookeeper.WatchedEvent;
import org.apache.zookeeper.Watcher;
import org.apache.zookeeper.ZooKeeper;
import org.apache.zookeeper.data.Stat;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;

import java.util.concurrent.CountDownLatch;

/**
 * @version: 1.0
 * @Description: TODO
 * @author: zZmH
 * @date: 2020/09/11 17:03
 */


public class ZKWatcherExists {

    public static final String ip = "192.168.190.128:2181";
    public static final int TIME_OUT = 5000;
    private ZooKeeper zooKeeper;

    @Before
    public void before() throws Exception {
        CountDownLatch latch = new CountDownLatch(1);
        zooKeeper = new ZooKeeper(ip, TIME_OUT, new Watcher() {
            @Override
            public void process(WatchedEvent watchedEvent) {
                if (watchedEvent.getState() == Event.KeeperState.SyncConnected) {
                    System.out.println("connection successfully");
                    latch.countDown();
                }
            }

        });
        latch.await();
    }

    @After
    public void after() throws Exception {
        zooKeeper.close();
    }

    @Test
    public void watcherExists1() throws KeeperException, InterruptedException {
        Stat exists = zooKeeper.exists("/node1", true);
        Thread.sleep(1000);

        System.out.println(exists.getVersion());
    }
    @Test
    public void watcherExists2() throws KeeperException, InterruptedException {
        zooKeeper.exists("/node1",
                new Watcher() {
                    @Override
                    public void process(WatchedEvent watchedEvent) {
                        System.out.println("diy watcher");
                        System.out.println(watchedEvent.getPath());
                        System.out.println("type: "+watchedEvent.getType());
                        System.out.println("State: "+watchedEvent.getState());
                    }
                });
        Thread.sleep(50000);
    }
    @Test
    public void watcherExists3() throws KeeperException, InterruptedException {
        Watcher watcher = new Watcher() {
            @Override
            public void process(WatchedEvent watchedEvent) {
                System.out.println("diy");
                System.out.println("path:" + watchedEvent.getPath());
                System.out.println("type:" + watchedEvent.getType());
                System.out.println("state:" + watchedEvent.getState());
                try {
                    zooKeeper.exists("/watcher1", this);
                } catch (KeeperException e) {
                    e.printStackTrace();
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        };
        zooKeeper.exists("/watcher1", watcher);
        Thread.sleep(50000);
        System.out.println("end");
    }
    @Test
    public void watcherExists4() throws KeeperException, InterruptedException {

    }


}